/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m12cqis01.nhathv.service;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author NhatHV
 */
public class Service {

    /**
     * The URL of the file we want to fetch.
     */
    private URL url;

    /**
     * Get the content of the given URL as a string.
     *
     * @param url The URL we want to get.
     * @return The content of the URL as a string.
     */
    public String getURL(String url) {
        this.url = this.parseURLString(url);
        URLConnection urlc = this.getURLConnection(this.url);
        return getContent(urlc);
    }

    /**
     * Gets the content of the given URL as an array of strings.
     *
     * @param address The URL of the file we want to read.
     * @return An array of strings containing the lines of the given file (URL).
     */
    public String[] getURLasLines(String address) {
        String all = this.getURL(address);
        all = all.replaceAll("\r", "\n");
        return all.split("\n");
    }

    /**
     * It can check if a given URL is alive or not.
     *
     * @param address URL address that we want to check if it's available or
     * not.
     * @return True, if it's available. False, otherwise.
     */
    public boolean checkHttpUrl(String address) {
        try {
            URL url = parseURLString(address);
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            int response = huc.getResponseCode();
            if (response == 200) {
                return true;
            } else {
                return false;
            }
        } catch (java.io.IOException ioe) {
        }
        return false;
    }

    /*
     * =======================================================================
     *                and here come the private functions
     * =======================================================================
     */
    /**
     * Get the content of a URL as a string.
     *
     * @param urlc URLConnection object the URL we want to read.
     * @return String content of the given URL.
     */
    private String getContent(URLConnection urlc) {
        try {
            InputStream is = urlc.getInputStream();
            DataInputStream ds = new DataInputStream(is);
            int length;
            length = urlc.getContentLength();
            if (length != -1) {
                byte b[] = new byte[length];
                ds.readFully(b);
                String s = new String(b);
                return s;
            } else {
                StringBuilder s = new StringBuilder();
                int i = is.read();
                while (i != -1) {
                    s.append((char) i);
                    i = is.read();
                }
                return new String(s);
            }
        } catch (IOException e) {
            System.err.println("Error: " + e.getMessage());
//            System.exit(-1);
        }
        return null;          // it cannot arrive here
    }

    /**
     * Checks if the URL is well-formed.
     *
     * @param address The URL we want to check.
     * @return The URL itself if it is correct. If it's not correct, the program
     * terminates.
     */
    private URL parseURLString(String address) {
        URL url = null;

        try {
            url = new URL(address);
        } catch (MalformedURLException e) {
            System.err.println("Error: the URL \"" + address + "\" is malformed!");
//            System.exit(-1);
        }
        return url;
    }

    /**
     * Gets a URLConnection object towards the given URL.
     *
     * @param url URL object representation of the file.
     * @return A URLConnection object towards the given URL.
     */
    private URLConnection getURLConnection(URL url) {
        URLConnection myUC = null;

        try {
            myUC = url.openConnection();
            myUC.connect();
        } catch (IOException e) {
            System.err.println("Error: network error while trying to open network connection!");
//            System.exit(-1);
        }
        return myUC;
    }

} // class MyGetUrl
